package cn.senjoeson.triper.activity;

import android.app.Dialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import cn.senjoeson.mylibrary.utils.TimestampUtils;
import cn.senjoeson.mylibrary.utilschanged.LogUtils;
import cn.senjoeson.mylibrary.utilschanged.MyToast;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.base.BaseActivity;
import cn.senjoeson.triper.base.GreenDaoManager;
import cn.senjoeson.triper.bean.Plan;
import cn.senjoeson.triper.dao.PlanDao;

/**
 * Created by senjoeson on 2017/12/30.
 */

public class SelectTripDateActivity extends BaseActivity {


    @BindView(R.id.left_arrow)
    ImageView mLeftArrow;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.right_arrow)
    ImageView mRightArrow;
    @BindView(R.id.toolbarlayout)
    LinearLayout mToolbarlayout;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.departDate)
    LinearLayout mDepartDate;
    @BindView(R.id.returnDate)
    LinearLayout mReturnDate;
    @BindView(R.id.setDepartDate)
    TextView mSetDepartDate;
    @BindView(R.id.setReturnDate)
    TextView mSetReturnDate;

    public static final int DEPART_DATE = 0;
    public static final int RETURN_DATE = 1;

    @Override
    protected void initView() {
        setSupportActionBar(mToolbar);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_trip_date;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }


    @OnClick({R.id.left_arrow, R.id.right_arrow, R.id.departDate, R.id.returnDate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_arrow:
                onBackPressed();
                break;
            case R.id.right_arrow:
                //先进行储存数据，然后跳转到主界面
                goHead();
                break;
            case R.id.departDate:
                shoCustomwDialog(DEPART_DATE);
                break;
            case R.id.returnDate:
                shoCustomwDialog(RETURN_DATE);
                break;
        }
    }

    private void goHead() {
        PlanDao planDao = GreenDaoManager.getInstance().getMaster().newSession().getPlanDao();
        String startDate = mSetDepartDate.getText().toString();
        String endDate = mSetReturnDate.getText().toString();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
        if (TextUtils.isEmpty(startDate) || TextUtils.isEmpty(endDate)) {
            MyToast.showToast("选择的日期不能为空");
            return;
        } else try {
            if (format.parse(startDate).after(format.parse(endDate))) {
                MyToast.showToast("返回日期要大于出发日期");
                return;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long days = TimestampUtils.getDays(startDate, endDate);

        LogUtils.d("startdate:" + startDate + "enddate" + endDate);
        Plan plan = new Plan();
        plan.setStartDate(startDate);
        plan.setEndDate(endDate);
        plan.setDays((int) (days+1));
        plan.setPlanId(new Random().nextLong());
        planDao.insert(plan);
        startActivity(MainActivity.class);  //重新进去主界面 刷新获取数据刷新页面
    }

    public void shoCustomwDialog(final int flag) {
        //获取日历的一个对象
        Calendar calendar = Calendar.getInstance();
        //获取年月日时分的信息
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        final Dialog dialog = new Dialog(this);
        dialog.setCanceledOnTouchOutside(false);

        dialog.setContentView(R.layout.select_date_dialog);
        DatePicker datePicker = dialog.findViewById(R.id.datePicker);
        TextView mCancel = dialog.findViewById(R.id.cancel);
        TextView mConfirm = dialog.findViewById(R.id.comfirm);
        setTitle(year + "-" + month + "-" + day);
        dialog.show();
        datePicker.init(year, month - 1, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                setTitle(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                if (flag == DEPART_DATE) {
                    mSetDepartDate.setText(getTitle().toString());
                } else {
                    mSetReturnDate.setText(getTitle().toString());
                }
            }
        });
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag == DEPART_DATE) {
                    mSetDepartDate.setText(getTitle().toString());
                } else {
                    mSetReturnDate.setText(getTitle().toString());
                }
                LogUtils.d(getTitle().toString());
                dialog.dismiss();
            }
        });
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                LogUtils.d(getTitle().toString());
            }
        });

    }



}



